import { api, LightningElement, wire } from 'lwc';
import { getRecord,getFieldValue } from 'lightning/uiRecordApi';

import NAME_FIELD from '@salesforce/schema/Account.Name';
import ACCOUNTNUMBER_FIELD from '@salesforce/schema/Account.AccountNumber';

const FIELDS = [
    NAME_FIELD,
    ACCOUNTNUMBER_FIELD
]

export default class CustomAccountViewer extends LightningElement {
    @api recordId;
    @api objectApiName;

    @wire(getRecord, { recordId: '$recordId', fields: FIELDS })
    currentAccount;

    get AccountName() {
        return getFieldValue(this.currentAccount.data, NAME_FIELD);
    }

    connectedCallback() {
        console.log('recordId: ' + this.recordId);
        console.log('objectApiName: ' + this.objectApiName);
    }
}



// 如果用非getFieldValue的方式，需要在页面判断currentAccount.data是否为空，因为页面渲染时数据还未加载回来
/* import { api, LightningElement, wire } from 'lwc';
import { getRecord } from 'lightning/uiRecordApi';

const FIELDS = [
    'Account.Name',
    'Account.AccountNumber'
]

export default class CustomAccountViewer extends LightningElement {
    @api recordId;
    @api objectApiName;

    @wire(getRecord, { recordId: '$recordId', fields: FIELDS })
    currentAccount;

    get AccountName() {
        return this.currentAccount.data.fields.Name.value;
    }

    connectedCallback() {
        console.log('recordId: ' + this.recordId);
        console.log('objectApiName: ' + this.objectApiName);
    }
} */